import {createContext, useContext, useReducer} from "react";
import {loginReducer} from "@/context/reducer/loginReducer.ts";
import {loginState} from "@/context/initialState/loginState.ts";
import type {ContextType, PropsType} from "@/types/commonType.ts";
import type {LoginStateType} from "@/types/loginType.ts";

const LoginContext = createContext<ContextType<LoginStateType>>({ state: loginState, dispatch: () => null })
// 1. 定义初始状态

export const LoginProvider = (props: PropsType) => {
    const [state, dispatch ] = useReducer(loginReducer, loginState)

    return <LoginContext.Provider value={{state, dispatch}}>{props.children}</LoginContext.Provider>
}

export const useLogin = () => {
    return useContext(LoginContext)
}